home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / LinAlg 3.1 / LinAlg / sample_adv.cc < prev    next >
C/C++ Source or Header  |  1995-12-21  |  4KB  |  112 lines

  1. //           Sample code showing off a few advanced features
  2. //      and comparing them (time-wise) with traditional ones
  3. //
  4. // Simple example: downsampling a matrix, that is, creating a matrix
  5. // that is 4 times (twice in each dimension) smaller than the original
  6. // matrix, by picking every other sample of the latter.
  7. //
  8. // $Id: sample_adv.cc,v 3.1 1995/02/07 14:40:37 oleg Exp oleg $
  9.  
  10. #include "LinAlg.h"
  11. #include <std.h>
  12. #include <iostream.h>
  13.  
  14.                 // Downsample matrix - new style
  15. class downsample_matrix : public LazyMatrix
  16. {
  17.   const Matrix& orig_matrix;
  18.   void fill_in(Matrix& m) const;
  19. public:
  20.   downsample_matrix(const Matrix& orig_matrix);
  21. };
  22.  
  23.                 // Just figure out the dimensions of the
  24.                 // downsampled (lazy) matrix
  25. downsample_matrix::downsample_matrix(const Matrix& _orig_matrix)
  26.     : LazyMatrix(_orig_matrix.q_row_lwb(),
  27.              (_orig_matrix.q_nrows()+1)/2 + _orig_matrix.q_row_lwb()-1,
  28.              _orig_matrix.q_col_lwb(),
  29.              (_orig_matrix.q_ncols()+1)/2 +_orig_matrix.q_col_lwb()-1),
  30.              orig_matrix(_orig_matrix)
  31. {
  32. }
  33.  
  34.                 // "construct" the new matrix (when the lazy
  35.                 // matrix is being "rolled out")
  36. void downsample_matrix::fill_in(Matrix& m) const
  37. {
  38.   class do_downsample : public ElementAction
  39.   {
  40.     const Matrix& orig_matrix;
  41.     const int row_lwb, col_lwb;
  42.     void operation(REAL& element)
  43.     { element = orig_matrix((i-row_lwb)*2+row_lwb,(j-col_lwb)*2+col_lwb); }
  44.     public: do_downsample(const Matrix& _orig_matrix)
  45.       : orig_matrix(_orig_matrix),
  46.     row_lwb(orig_matrix.q_row_lwb()),
  47.     col_lwb(orig_matrix.q_col_lwb()) {}
  48.   };
  49.   m.apply(do_downsample(orig_matrix));
  50. }
  51.  
  52.                 // Downsample in the traditional style
  53. static Matrix traditional_downsampling(const Matrix& orig_matrix)
  54. {
  55.   Matrix smaller_m(orig_matrix.q_row_lwb(),
  56.            (orig_matrix.q_nrows()+1)/2 + orig_matrix.q_row_lwb()-1,
  57.            orig_matrix.q_col_lwb(),
  58.            (orig_matrix.q_ncols()+1)/2 + orig_matrix.q_col_lwb()-1);
  59.  
  60.   for(register int i=0; i<smaller_m.q_nrows(); i++)
  61.     for(register int j=0; j<smaller_m.q_ncols(); j++)
  62.       smaller_m(i+smaller_m.q_row_lwb(),j+smaller_m.q_col_lwb()) =
  63.     orig_matrix(2*i+smaller_m.q_row_lwb(),2*j+smaller_m.q_col_lwb());
  64.   return smaller_m;
  65. }
  66.  
  67. main(void)
  68. {
  69.   cout << "\nDonsample matrices using traditional and non-traditional methods"
  70.     << endl;
  71.  
  72.   {
  73.     cout << "\nMake sure that both methods give the same results" << endl;
  74.     Matrix orig_m = haar_matrix(9,201);    // which is a pretty big matrix
  75.     Matrix small1 = traditional_downsampling(orig_m);
  76.     Matrix small2 = downsample_matrix(orig_m);
  77.     assert( small1 == small2 );
  78.   }
  79.  
  80.   {
  81.     cout << "\nClock the traditional downsampling" << endl;
  82.     start_timer();
  83.     for(int order=1; order<=9; order++)
  84.     {
  85.       Matrix orig_m = haar_matrix(order);    // may be pretty big, btw
  86.       for(int count=0; count < (1<<(12-order)); count++)
  87.       {
  88.     Matrix small = traditional_downsampling(orig_m);
  89.     small(1,1) = 1;                // just to use the matrix
  90.       }
  91.     }
  92.     cout << "\tIt took " << return_elapsed_time(0) 
  93.          << " sec to complete the test" << endl;
  94.   }
  95.  
  96.   {
  97.     cout << "\nClock the 'new style' downsampling (with lazy matrices)"<< endl;
  98.     start_timer();
  99.     for(int order=1; order<=9; order++)
  100.     {
  101.       Matrix orig_m = haar_matrix(order);    // may be pretty big, btw
  102.       for(int count=0; count < (1<<(12-order)); count++)
  103.       {
  104.     Matrix small = downsample_matrix(orig_m);
  105.     small(1,1) = 1;                // just to use the matrix
  106.       }
  107.     }
  108.     cout << "\tIt took " << return_elapsed_time(0) 
  109.          << " sec to complete the test" << endl;
  110.   }
  111. }    
  112.